package com.platform.core.web.controller;

import com.platform.comm.enums.IEnumOption;
import com.platform.comm.enums.YnEnum;
import com.platform.comm.web.results.DataJsonResult;
import com.platform.enums.*;
import com.platform.enums.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.platform.core.web.controller.BaseController.enum2Map;

/**
 * Description: 返回系统枚举值与其描述
 *
 * @author libin
 * Created on 2019/4/26
 **/
@RestController
@RequestMapping("management/enum")
public class EnumMapController {

    @Qualifier("enumMapper")
    @Autowired
    private Map<String, Class<? extends IEnumOption>> enumMapper;

    @PostMapping(name = "统一枚举接口", path = "/common")
    public DataJsonResult<Map<String, Object>> common(@RequestBody List<String> enumNames) {
        Map<String, Object> params = enumNames.stream().filter(enumMapper::containsKey)
                .collect(Collectors.toMap(key -> key, key -> enumMapper.get(key).getEnumConstants()[0].enums()));

        return DataJsonResult.succeed(params);
    }

    @PostMapping(name = "统一枚举接口", path = "/common/lkv")
    public DataJsonResult<Map<String, List<Map<String, Object>>>> commonLkv(@RequestBody List<String> enumNames) {
        Map<String, List<Map<String, Object>>> params = new HashMap<>(4);
        Map<String, Class<? extends IEnumOption>> stringClassMap = enumMapper;
        for (String key : enumNames) {
            if (stringClassMap.containsKey(key)) {
                Map<String, Object> map = enumMapper.get(key).getEnumConstants()[0].enums();
                params.put(key, toListMap(map));
            }
        }

        return DataJsonResult.succeed(params);
    }

    private List<Map<String, Object>> toListMap(Map<String, Object> map) {
        List<Map<String, Object>> listmap = new ArrayList<>();
        map.forEach((key, value) -> {
            Map<String, Object> nm = new HashMap<>(1);
            nm.put(key, value);
            listmap.add(nm);
        });
        return listmap;
    }

    @PostMapping(name = "通用状态枚举", path = "/commStatus")
    public DataJsonResult<Map<String, String>> getCommFlagStatus(@RequestBody Map<String, String> map) {
        return DataJsonResult.succeed(CommFlagType.getCommFlagStatus(map.get("statusType")));
    }

    @GetMapping(name = "通用状态枚举", path = "/yn")
    public DataJsonResult<Map<String, String>> getYn() {
        return DataJsonResult.succeed(BaseController.enum2Map(YnEnum.class, YnEnum::getText));
    }

    @GetMapping(name = "参数取值模式", path = "/valueMode")
    public DataJsonResult<Map<String, String>> getValueMode() {
        return DataJsonResult.succeed(ConfValueMode.getAllValueModes());
    }

    @GetMapping(name = "菜单类型枚举", path = "/menuType")
    public DataJsonResult<Map<String, String>> getFunctionTypes() {
        return DataJsonResult.succeed(FunctionType.getAllValues());
    }

    @GetMapping(name = "角色类型枚举", path = "/roleType")
    public DataJsonResult<Map<String, String>> getRoleType() {
        return DataJsonResult.succeed(BaseController.enum2Map(RoleType.class, RoleType::getText));
    }

    @GetMapping(name = "范围类型枚举", path = "/rangeType")
    public DataJsonResult<Map<String, String>> getRangeType() {
        return DataJsonResult.succeed(BaseController.enum2Map(RangeType.class, RangeType::getText));
    }

    @GetMapping(name = "字典类型枚举", path = "/dictType")
    public DataJsonResult<Map<String, String>> getDictType() {
        return DataJsonResult.succeed(BaseController.enum2Map(DictType.class, DictType::getText));
    }

    @GetMapping(name = "部门类型枚举", path = "/departType")
    public DataJsonResult<Map<String, String>> getDepartType() {
        return DataJsonResult.succeed(BaseController.enum2Map(DepartType.class, DepartType::getText));
    }

    @GetMapping(name = "接口类型枚举", path = "/apiType")
    public DataJsonResult<Map<String, String>> getApiType() {
        return DataJsonResult.succeed(BaseController.enum2Map(ApiType.class, ApiType::getText));
    }
}
